package com.wiscamp.ninechapters.problems.adapter.items;

import com.wiscamp.ninechapters.problems.application.dto.commands.problems.CreateOptionDto;
import com.wiscamp.ninechapters.problems.application.dto.commands.problems.CreateProblemCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.problems.CreateQuestionDto;
import com.wiscamp.ninechapters.problems.application.dto.commands.problems.CreateSolutionDto;
import com.wiscamp.ninechapters.problems.application.services.ProblemAppService;
import com.wiscamp.ninechapters.problems.application.services.ProblemCollectionAppService;
import com.wiscamp.ninechapters.problems.domain.models.collections.CollectionProblem;
import com.wiscamp.ninechapters.problems.domain.models.collections.ProblemCollection;
import com.wiscamp.ninechapters.problems.domain.repositories.ProblemCollectionRepository;
import com.wiscamp.ninechapters.problems.domain.services.ProblemCollectionService;
import com.wiscamp.ninechapters.problems.domain.services.ProblemService;
import com.wiscamp.ninechapters.problems.enums.ProblemModes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;

@Service
@Deprecated
public class ItemService {

    @Autowired
    private ProblemCollectionRepository problemCollectionRepository;

    @Autowired
    private ProblemCollectionService problemCollectionService;

    @Autowired
    private ProblemCollectionAppService problemCollectionAppService;

    @Autowired
    private ProblemService problemService;

    @Autowired
    private ProblemAppService problemAppService;

    public ItemPoolDto getItemPoolByCode(String code) {
        RestTemplate template = new RestTemplate();
        String apiUrl = "http://itembank.hybleland.com:8000/organizations/1/pools/code/{0}";
        String url =  MessageFormat.format(apiUrl, code);
        ResponseEntity<ItemPoolResponse> response = template.getForEntity(url, ItemPoolResponse.class);

        if (response.hasBody()) {
            ItemPoolResponse itemPoolResponse = response.getBody();
            Object object = itemPoolResponse.getData();
            return (ItemPoolDto) object;
        }
        return null;
    }


    public Collection<ItemDto> getItemsById(long poolId) {
        RestTemplate template = new RestTemplate();
        String url = MessageFormat.format("http://itembank.hybleland.com:8000/organizations/1/pools/{0}/items", String.valueOf(poolId));
        ResponseEntity<ItemsResponse> response = template.getForEntity(url, ItemsResponse.class);

        if (response.hasBody()) {
            ItemsResponse itemsResponse = response.getBody();
            Object object = itemsResponse.getData();
            return (Collection<ItemDto>)object;
        }
        return new ArrayList<>();
    }

    public boolean CopyItemPool(String poolCode) {
        var existPool = problemCollectionRepository.getProblemCollectionByCode(poolCode, 1);
        if (existPool != null) return false;

        ItemPoolDto itemPool = getItemPoolByCode(poolCode);
        if (itemPool == null) return false;

        var newCollection = new ProblemCollection();
        BeanUtils.copyProperties(itemPool, newCollection);
        newCollection.setCollectionCode(itemPool.getPoolCode());
        newCollection.setCollectionName(itemPool.getPoolName());
        if (newCollection.getCollectionName().contains("AMC8"))
            newCollection.setExamId(510);
        if (newCollection.getCollectionName().contains("AMC10"))
            newCollection.setExamId(520);
        if (newCollection.getCollectionName().contains("AMC12"))
            newCollection.setExamId(530);
        newCollection.setCollectionCategoryId(148014);
        newCollection.setTotalScore(itemPool.getTotalScore());
        newCollection.setSubjectId(10);

        newCollection.setProblemQuantity(itemPool.getItemQuantity());
        newCollection.setLanguageCode(itemPool.getLanguageCode());

        // create problem collection
        newCollection = problemCollectionService.createProblemCollection(newCollection, 0);

        Collection<ItemDto> items = getItemsById(itemPool.getPoolId());
        if (items.isEmpty()) return false;

        for (ItemDto item : items) {
            var newProblem = new CreateProblemCommand();
            var newCollectionProblem = new CollectionProblem();
            BeanUtils.copyProperties(item, newProblem);
            newProblem.setProblemTypeId(20);
            newProblem.setProblemMode(ProblemModes.SIMPLE);
            newProblem.setCollectionId(newCollection.getCollectionId());

            Collection<CreateQuestionDto> questions = new ArrayList<>();
            for (QuestionDto questionDto : item.getQuestions()) {
                var newQuestion = new CreateQuestionDto();
                BeanUtils.copyProperties(questionDto, newQuestion);
                newQuestion.setQuestionHypertext(questionDto.getQuestionHypertext());
                newQuestion.setQuestionTypeId(2050);

                // solutions
                var solutions = new ArrayList<CreateSolutionDto>();
                var solutionIndex = 1;
                var tempList = new ArrayList<QuestionAnalysisDto>();
                tempList.add(questionDto.getAnalysis());
                for (QuestionAnalysisDto analysisDto : tempList) {
                    var newSolution = new CreateSolutionDto();
                    BeanUtils.copyProperties(analysisDto, newSolution);
                    newSolution.setSolutionHypertext(analysisDto.getAnalysisHypertext());
                    newSolution.setPreferred(true);
                    newSolution.setSolutionIndex(solutionIndex);
                    solutions.add(newSolution);
                    solutionIndex++;
                }
                newQuestion.setSolutions(solutions);

                // options
                var options = new ArrayList<CreateOptionDto>();
                var optionIndex = 1;
                for (QuestionOptionDto optionDto : questionDto.getOptions()) {
                    var newOption = new CreateOptionDto();
                    BeanUtils.copyProperties(optionDto, newOption);
                    newOption.setOptionHypertext(optionDto.getOptionHypertext());
                    newOption.setOptionIndex(optionIndex);
                    newOption.setCorrectAnswer(optionDto.isCorrectAnswer());
                    options.add(newOption);
                    optionIndex++;
                }
                newQuestion.setOptions(options);

                // answers
                /*var newAnswers = new ArrayList<Answer>();
                var answerIndex = 1;
                for (QuestionAnswerDto answerDto : questionDto.getAnswers()) {
                    var newAnswer = new Answer();
                    BeanUtils.copyProperties(answerDto, newAnswer);
                    newAnswer.setAnswerHypertext(answerDto.getContentHypertext());
                    newAnswer.setAnswerIndex(answerIndex);
                    newAnswer.setAnswerOptionId(0);
                    newAnswer.setAction(Actions.CREATE);
                    newAnswers.add(newAnswer);
                    answerIndex++;
                }
                newQuestion.setCurrentAnswers(newAnswers);*/

                questions.add(newQuestion);
            }

            newProblem.getQuestions().addAll(questions);
            //newProblem.setCurrentQuestion(questions.stream().findFirst().orElse(null));
            //newProblem.setQuestion(questions.stream().findFirst().orElse(null));
            // create problem
            var problemVO = problemAppService.createProblem(newProblem);
            System.out.println(MessageFormat.format("Creating Item Id: {0}", problemVO.getProblemId()));
        }

        return true;
    }
}
